package cn.sytton.taffecloud.service.log.service.impl;

import cn.sytton.taffecloud.common.base.util.SecurityUtil;
import cn.sytton.taffecloud.common.db.model.PageParam;
import cn.sytton.taffecloud.common.servlet.service.BaseService;
import cn.sytton.taffecloud.service.log.entity.UserLogEntity;
import cn.sytton.taffecloud.service.log.mapper.UserLogMapper;
import cn.sytton.taffecloud.service.log.service.IUserLogService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;

/**
 * 用户操作日志服务层实现
 *
 * @author skyrock
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserLogService extends BaseService<UserLogEntity> implements IUserLogService {

    @Resource
    private UserLogMapper userLogMapper;

    @Override
    public void create(UserLogEntity entity) {
        entity.setUserId(SecurityUtil.getUserId());
        entity.setUserCode(SecurityUtil.getUserCode());
        entity.setUserName(SecurityUtil.getUserName());
        entity.setLoginName(SecurityUtil.getLoginName());
        entity.setAction(StringUtils.hasText(entity.getAction()) ? entity.getAction() : "打开菜单");
        entity.setLogTime(new Timestamp(new Date().getTime()));
        entity.setIp(SecurityUtil.getIp());
        entity.setLocation(SecurityUtil.getLoginLocation());
        entity.setBrowser(SecurityUtil.getBrowser());
        entity.setOs(SecurityUtil.getOs());
        userLogMapper.create(entity);
    }

    @Override
    public PageInfo<UserLogEntity> findListPage(PageParam pp) {
        setPageParams(pp);
        return new PageInfo<>(userLogMapper.findList(pp.getParams()));
    }
}
